//
//  LayerHall.cpp
//  lhq
//
//  Created by codin on 14-7-24.
//
//

#include "LayerHall.h"
#include "ui/CocosGUI.h"
#include "LayerReady.h"
#include "LayerTable.h"
USING_NS_CC;
using namespace ui;

Scene* LayerHall::createScene() {
    auto scene = Scene::create();
    auto layer = LayerHall::create();
    scene->addChild(layer);
    return scene;
}

bool LayerHall::init() {
    if (!Layer::init()) {
        return false;
    }
    
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/icons/icons.plist", "texture/icons/icons.png");
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/commonHeader.plist", "texture/commonHeader.png");
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/hall/hall.plist", "texture/hall/hall_big.png");
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/hall/entryBtns.plist", "texture/hall/entryBtns.png");
    
    auto director = Director::getInstance();
    Size visibleSize = director->getVisibleSize();
    Vec2 origin = director->getVisibleOrigin();
    
    auto bgSp = Sprite::create("texture/hall/hall_bg.jpg");
    bgSp->setPosition(Vec2(origin.x + visibleSize.width - bgSp->getContentSize().width/2,origin.y + bgSp->getContentSize().height/2));
    this->addChild(bgSp);
    
    this->addHead();
    this->addRankBg();
    this->addTipList();
    this->addEntryBtns();
    
    return true;
}

bool LayerHall::addHead() {
    auto headerBgSp = Sprite::createWithSpriteFrameName("headback.png");
    headerBgSp->setPosition(Vec2(100,Director::getInstance()->getVisibleSize().height-50));
    this->addChild(headerBgSp);
    
//    auto account = app.getPlayerAccount();
//    //        log("playerInfo=%s",JSON.stringify(playerInfo));
    
    auto img = StringUtils::format("icon_%s_%d.jpg","f",1);
    auto playerIconSp = Sprite::createWithSpriteFrameName(img);
    playerIconSp->setPosition(Vec2(83,Director::getInstance()->getVisibleSize().height-37));
    playerIconSp->setScale(0.55);
    this->addChild(playerIconSp);
    
    auto nameBgSp = Sprite::createWithSpriteFrameName("infoback2.png");
    nameBgSp->setPosition(Vec2(250,Director::getInstance()->getVisibleSize().height-40));
    this->addChild(nameBgSp);
    
    auto nameLb = LabelTTF::create("玩家名.","Helvetica",32);
    nameLb->setPosition(Vec2(80,40));
    nameBgSp->addChild(nameLb);
    
    auto vmBgSp = Sprite::createWithSpriteFrameName("infoback1.png");
    vmBgSp->setPosition(Vec2(Director::getInstance()->getVisibleSize().width/2+200,Director::getInstance()->getVisibleSize().height-40));
    this->addChild(vmBgSp);
    
    auto vmIconSp = Sprite::createWithSpriteFrameName("main_happybean.png");
    vmIconSp->setPosition(Vec2(40,40));
    vmIconSp->setScale(1.2);
    vmBgSp->addChild(vmIconSp);
    
    auto vmLb = LabelTTF::create("100","Helvetica",32);
    vmLb->setPosition(Vec2(140,35));
    vmBgSp->addChild(vmLb);
    
    auto rmIconSp = Sprite::createWithSpriteFrameName("main_diamond.png");
    rmIconSp->setPosition(Vec2(340,40));
    rmIconSp->setScale(1.2);
    vmBgSp->addChild(rmIconSp);
    
    auto rmLb = LabelTTF::create("100","Helvetica",32);
    rmLb->setPosition(Vec2(440,35));
    vmBgSp->addChild(rmLb);
  
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    Size size = Director::getInstance()->getVisibleSize();
    
    auto vmBuySp = Sprite::create("texture/shop/buy_btn.png");
    vmBuySp->setPosition(Vec2(size.width/2+150,size.height-40));
    vmBuySp->setScale(0.6);
    this->addChild(vmBuySp);
    
    auto rmBuySp = Sprite::create("texture/shop/buy_btn.png");
    rmBuySp->setPosition(Vec2(size.width/2+450,size.height-40));
    rmBuySp->setScale(0.6);
    this->addChild(rmBuySp);
    
    // Make vmBuySp touchable
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = [](Touch* touch, Event* event){
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        
        Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
        Size s = target->getContentSize();
        Rect rect = Rect(0, 0, s.width, s.height);
        
        if (rect.containsPoint(locationInNode)){
            log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
            target->setOpacity(180);
            return true;
        }
        return false;
    };
    
    listener->onTouchMoved = [](Touch* touch, Event* event){
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        target->setPosition(target->getPosition() + touch->getDelta());
    };
    
    listener->onTouchEnded = [=](Touch* touch, Event* event){
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        log("sprite onTouchesEnded.. ");
        target->setOpacity(255);
    };
    
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,vmBuySp);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),rmBuySp);
    
    return true;
}

bool LayerHall::addRankBg() {
    auto rankBgSp = Sprite::createWithSpriteFrameName("rank_bg.png");
    rankBgSp->setPosition(Vec2(300,300));
    this->addChild(rankBgSp);
    
    auto toggleBtn = Sprite::createWithSpriteFrameName("toggle_btn.png");
    toggleBtn->setPosition(Vec2(164,520));
    this->addChild(toggleBtn);
    
    auto toggleBtn2 = Sprite::createWithSpriteFrameName("toggle_btn_2.png");
    toggleBtn2->setPosition(Vec2(342,520));
    this->addChild(toggleBtn2);
    
    auto titleLb = LabelTTF::create("公   告.","Helvetica",30);
    titleLb->setPosition(Vec2(164,515));
    this->addChild(titleLb);
    
    return true;
}

bool LayerHall::addTipList() {
    ListView* listView = ListView::create();
    listView->setDirection(ScrollView::Direction::VERTICAL);
    listView->setBounceEnabled(true);
//    listView->setBackGroundImage("cocosui/green_edit.png");
//    listView->setBackGroundImageScale9Enabled(true);
    listView->setAnchorPoint(Vec2(0.5, 0.5));
    listView->setContentSize(Size(380, 300));
    listView->setPosition(Vec2(300,Director::getInstance()->getVisibleSize().height/2-80));
    this->addChild(listView);
    
    std::string tips[] = {
        "1.反馈建议,免费获得钻石",
        "2.客服qq：229259695",
        "3.玩家qq群：241063526",
        "4.首次充值，获得超值礼包."
    };
    
    for (int i = 0; i < 4; i++) {
        Layout* layoutItem = Layout::create();
        layoutItem->setTouchEnabled(true);
        
        layoutItem->setContentSize(Size(400,50));

        auto tipLb = LabelTTF::create(tips[i],"Helvetica",30);
        tipLb->setPosition(Vec2(layoutItem->getContentSize().width/2.0f, layoutItem->getContentSize().height/2.0f));
        layoutItem->addChild(tipLb);
        
        listView->pushBackCustomItem(layoutItem);
    }

    listView->setGravity(ListView::Gravity::CENTER_VERTICAL);
    listView->setItemsMargin(10);
    
    return true;
}

bool LayerHall::addEntryBtns() {
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = [](Touch* touch, Event* event){
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        
        Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
        Size s = target->getContentSize();
        Rect rect = Rect(0, 0, s.width, s.height);
        
        if (rect.containsPoint(locationInNode)){
            log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
            target->setOpacity(180);
            return true;
        }
        return false;
    };
    
    listener->onTouchMoved = [](Touch* touch, Event* event){
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        target->setPosition(target->getPosition() + touch->getDelta());
    };
    
    listener->onTouchEnded = [=](Touch* touch, Event* event){
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        log("sprite onTouchesEnded.. ");
        target->setOpacity(255);
		Director::getInstance()->replaceScene(LayerReady::createScene());
		

    };
    
    auto entry0 = Sprite::createWithSpriteFrameName("entry_btn_0.png");
    entry0->setPosition(Vec2(680,450));
    this->addChild(entry0);
    auto difenLb0 = LabelTTF::create("底分 50","Helvetica",30);
    difenLb0->setPosition(Vec2(90,40));
    entry0->addChild(difenLb0);
    auto vmLb0 = LabelTTF::create("1千以上豆","Helvetica",16);
    vmLb0->setPosition(Vec2(90,15));
    entry0->addChild(vmLb0);
    
    auto entry1 = Sprite::createWithSpriteFrameName("entry_btn_1.png");
    entry1->setPosition(Vec2(900,450));
    this->addChild(entry1);
    auto difenLb1 = LabelTTF::create("底分 150","Helvetica",30);
    difenLb1->setPosition(Vec2(90,40));
    entry1->addChild(difenLb1);
    auto vmLb1 = LabelTTF::create("5千以上豆","Helvetica",16);
    vmLb1->setPosition(Vec2(90,15));
    entry1->addChild(vmLb1);
    
    auto entry2 = Sprite::createWithSpriteFrameName("entry_btn_2.png");
    entry2->setPosition(Vec2(680,200));
    this->addChild(entry2);
    auto difenLb2 = LabelTTF::create("底分 500","Helvetica",30);
    difenLb2->setPosition(Vec2(90,40));
    entry1->addChild(difenLb2);
    auto vmLb2 = LabelTTF::create("2万以上豆","Helvetica",16);
    vmLb2->setPosition(Vec2(90,15));
    entry2->addChild(vmLb2);
    
    auto entry3 = Sprite::createWithSpriteFrameName("entry_btn_3.png");
    entry3->setPosition(Vec2(900,200));
    this->addChild(entry3);
    auto difenLb3 = LabelTTF::create("底分 2000","Helvetica",30);
    difenLb3->setPosition(Vec2(90,40));
    entry3->addChild(difenLb3);
    auto vmLb3 = LabelTTF::create("10万以上豆","Helvetica",16);
    vmLb3->setPosition(Vec2(90,15));
    entry3->addChild(vmLb3);
    
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,entry0);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),entry1);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),entry2);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),entry3);
    
    return true;
}


